package com.hs.biye.aop;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hs.biye.annotation.AuthCheck;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.model.entity.Employee;
import com.hs.biye.service.EmployeeService;
import com.hs.biye.utils.BaseContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Aspect
@Component
public class AuthInterceptor {
    @Resource
    private EmployeeService employeeService;



    /**
     * 执行拦截
     *
     * @param joinPoint
     * @param authCheck
     * @return
     */

    @Around("@annotation(authCheck)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
        String mustRole = authCheck.mustRole();
        // 当前登录用户
        Long currentId = BaseContext.getCurrentId();
        Employee employee = employeeService.getById(currentId);
        if (employee == null){
            throw new BaseException(MessageConstant.OUT_LOGIN);
        }
        String userRoler = employee.getRoler();
        // 必须有该权限才通过
        if (StringUtils.isNotBlank(mustRole)) {
            if (!mustRole.equals(userRoler)){
                throw new BaseException("");
            }
        }
        // 通过权限校验，放行
        return joinPoint.proceed();
    }

}

